package com.foreverwin.mes.lims.controller;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.exception.BusinessException;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.foreverwin.mes.core.utils.StringUtils;
import com.foreverwin.mes.lims.dto.InspectTaskDto;
import com.foreverwin.mes.lims.enums.InspectObject;
import com.foreverwin.mes.lims.util.DataValueConstant;
import com.foreverwin.mes.lims.vo.InspectTaskVo;
import com.foreverwin.mes.meapi.model.Resrce;
import com.foreverwin.mes.meapi.service.ResrceService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mes.lims.service.InspectTaskService;
import com.foreverwin.mes.lims.model.InspectTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 *
 * @author Levy
 * @since 2020-02-24
 */
@Controller
@RequestMapping("/inspectTasks")
public class InspectTaskController {
    private final Logger logger = LoggerFactory.getLogger(InspectTaskController.class);

    @Autowired
    public InspectTaskService inspectTaskService;
    @Autowired
    private ResrceService resrceService;

    @ResponseBody
    @RequestMapping(value = "/doSave", method = RequestMethod.POST)
    public AjaxResult stationCertificationSave(@RequestBody InspectTaskVo inspectTaskVo) {

        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();

        try {
            inspectTaskVo.setSite( site );
            inspectTaskVo.setUser( user );

            inspectTaskService.doSave( inspectTaskVo );
        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }

        return AjaxResult.success("检验任务创建成功！");
    }

    @ResponseBody
    @GetMapping(value = "/Search")
    public AjaxResult getInspectTask( String object , String workCenter ,String resrce) {

        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        List<InspectTaskDto> inspectTasks;

        try {
            if (InspectObject.ENV.name().equals( object )){
                if ( StringUtils.isBlank( workCenter ) ){
                 throw BusinessException.build("请选择工作中心!");
                }
            }else {
                if ( StringUtils.isBlank( resrce ) ){
                    throw BusinessException.build("请选择设备编号!");
                }
            }
            inspectTasks = inspectTaskService.doSearch( object , workCenter ,resrce ,site ,user);
        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }

        return AjaxResult.success(inspectTasks);
    }

    @ResponseBody
    @RequestMapping(value = "doDelete", method = RequestMethod.POST)
    public AjaxResult stationDelete(@RequestBody JSONObject jsonObject) {

        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        String result = "";
        boolean count = true;

        try {
            String task = jsonObject.getString("task");
            if (StringUtils.isBlank(task)) {
                throw BusinessException.build("任务编号不能为空!");
            }

            InspectTask inspectTask = inspectTaskService.selectOne(
                    new EntityWrapper<InspectTask>()
                    .eq("TASK_ID",task)
                    .eq("SITE",site)

            );
            if ( inspectTask != null ){
                inspectTask.setTaskStatus(DataValueConstant.TASK_STATUS_DELETE);
                inspectTask.setUpdatedDateTime( new Date());
                inspectTask.setUpdatedUser( user );
                count = inspectTaskService.updateById( inspectTask );
            }else {
                count = false;
            }
            result = count == true ? "删除成功" : "删除失败";

        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }

        return AjaxResult.success(result);
    }

    @ResponseBody
    @GetMapping("")
    public AjaxResult getResrceList(Resrce resrce){
        List<Resrce> resrces;
        List< Map > result = new ArrayList<>();
        try {
            EntityWrapper<Resrce> EntityWrapper = new EntityWrapper<>();
            resrce.setSite(CommonMethods.getSite());
            EntityWrapper.setEntity(resrce);
            resrces = resrceService.selectList(EntityWrapper);
            if ( !resrces.isEmpty() ){
                for ( Resrce r : resrces ){
                    result.add( inspectTaskService.doSearchItemByResrce( r ) );
                }
            }

        } catch (Exception e) {
            logger.error("getResrceList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success( result );
    }

}